package com.airforce.controller;

import com.airforce.mappers.user_awardMapper;
import com.airforce.mappers.user_topicMapper;
import com.airforce.mappers.wechat_userMapper;
import com.airforce.model.wechat_user;
import com.airforce.utils.ResultCode;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(value = "/system")
public class Wehar_userCon_admin {


    private  final static Logger logger=LoggerFactory.getLogger(Wehar_userCon_admin.class);

    @Autowired
    wechat_userMapper wechat_userMapper;


    @Autowired
    user_awardMapper user_awardMapper;

    @Autowired
    user_topicMapper user_topicMapper;




    /**
     * 查询用户信息
     * @param uuid
     * @param index
     * @param num
     * @return
     */
    @GetMapping(value = "/userlist")
    public Object userlist(@RequestParam(required = false) String uuid,
                           @RequestParam(required = false) int index ,
                           @RequestParam(required = false) int num
    ) {

        List<wechat_user> list = new ArrayList<>();
        int PageTotal=0;
        try {

            if(uuid==null||uuid==""){

                //获得数据总条数
                PageTotal=wechat_userMapper.countmax();
                //进行分页结果查询
                PageHelper.startPage(index,num);
                list=wechat_userMapper.selectByall();
            }else {
                PageTotal=wechat_userMapper.selectBylist(uuid).size();
                list = wechat_userMapper.selectBylist(uuid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultCode.SUCCESS.result(list,PageTotal);
    }



    @GetMapping(value = "finduser")
    public Object finduser(@RequestParam Integer id){

        if(id!=null){

            wechat_user buyer=wechat_userMapper.selectByPrimaryKey(id);
            return ResultCode.SUCCESS.result(buyer);
        }else {
            return ResultCode.Empty_Selct.result();
        }
    }



    //删除用户
    @RequestMapping(value = "deleBy",method = RequestMethod.GET)
    public Object deleBy(@RequestParam String openid)
    {

        try {
            if(Optional.ofNullable(openid).isPresent()) {
                logger.info("删除用户：" + openid);
                wechat_userMapper.deleteByOpenid(openid);
             wechat_user user=   wechat_userMapper.selectByopenid(openid);
//                String openid = wechat_userMapper.selectByPrimaryKey(id).getOpenid();
                logger.info("清空用户:" + user.getUuid() + "的获奖数据");
                user_awardMapper.deleteByopenid(openid);
                logger.info("清空用户:" + user.getUuid() + "的答题记录");
                user_topicMapper.deleteByOpenid(openid);
                return ResultCode.SUCCESS.result();
            }else {
                return ResultCode.NO_PARM.result();
            }
        }catch (Exception e)
        {
            return ResultCode.DELETEDEBT_ERROR.result();
        }

    }


    //更新用户

    @PostMapping(value = "update")
    public  Object updateBy(@RequestBody wechat_user buyer){

        if(buyer!=null){

            wechat_userMapper.updateByPrimaryKeySelective(buyer);
            return ResultCode.SUCCESS.result();
        }else {

            return ResultCode.UNKNOWN_ERROR.result();
        }

    }

    //更新状态
//    @GetMapping(value = "reditstatus")
//    public  Object rediststatus(@ApiParam(value = "买家id")@RequestParam Integer buyerid){
//        String status_1="0";
//        String status_2="1";
//        buyer_info buyer=new buyer_info();
//        if(buyerid!=null){
//            String status=buyer_infoMapper.selectByPrimaryKey(buyerid).getBuyerstatus();
//            System.out.println("状态值为"+status);
//            if(status.equals(status_1)){
//                System.out.println("进行修改状态----1");
//                buyer.setBuyerid(buyerid);
//                buyer.setBuyerstatus(status_2);
//                buyer_infoMapper.rediststatus(buyer);
//            }else {
//                buyer.setBuyerid(buyerid);
//                buyer.setBuyerstatus(status_1);
//                buyer_infoMapper.rediststatus(buyer);
//            }
//            return ResultCode.SUCCESS.result();
//        }
//        return ResultCode.UNKNOWN_ERROR.result();
//
//    }




}


